import { getBaseUrl, requestUtil } from '../../utils/requestUtil.js'
import regeneratorRuntime from "../../lib/runtime/runtime"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList: [], // 左侧菜单数据
    rightContext: [], // 右侧菜单数据
    baseUrl: '',
    currentIndex: 0,
    scrollTop: 0,
  },

  Cates: [], // 所有商品类别数据

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const baseUrl = getBaseUrl();
    this.setData({
      baseUrl
    })

    this.getCates()
  },

  handleMenuItemChange(e) {
    const { index } = e.currentTarget.dataset
    let rightContext = this.Cates[index].smallTypeList
    this.setData({
      currentIndex: index,
      rightContext,
      scrollTop: 0
    })
  },

  async getCates() {
    const result = await requestUtil({
      url: '/bigType/findCategories',
      method: "GET"
    });
    this.Cates = result.message
    let leftMenuList = this.Cates.map(item => item.name)
    let rightContext = this.Cates[0].smallTypeList
    this.setData({
      leftMenuList,
      rightContext
    })
  },

  async getCates2(index) { // 从首页跳转
    const baseUrl = getBaseUrl();
    const result = await requestUtil({
      url: '/bigType/findCategories',
      method: "GET"
    });
    this.Cates = result.message
    let leftMenuList = this.Cates.map(item => item.name)
    let rightContext = this.Cates[index].smallTypeList
    this.setData({
      leftMenuList,
      rightContext,
      currentIndex: index,
      scrollTop: 0,
      baseUrl
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const app = getApp()
    const { index } = app.globalData
    
    if (index !== -1) { // 从首页跳转
      this.getCates2(index)
      app.globalData.index = -1 // 重置index
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})